import {createSlice} from "@reduxjs/toolkit"
import axios from "axios"

const channelStore = createSlice({
    name:'channel',
    initialState:{
        channelList:[]
    },
    reducers:{
        setChannelList(state,action){
            state.channelList = action.payload
        }
    }
})

//创建异步
const {setChannelList}  = channelStore.actions
const url = 'http://localhost:3004/channels'
const fetchChannelList = ()=>{
    return async (dispatch)=>{
        const res = await axios.get(url)
        dispatch(setChannelList(res.data))
    }
}

export {fetchChannelList}
const channelReducer = channelStore.reducer
export default channelReducer